import { ref } from 'vue';
import { createGlobalState } from '@vueuse/core';
import { addDisableProperty, listToEnum, listToOption } from '@/utils/utils';
import { getDictType } from '@/api/monitor';
import { getModel, getProjectTree, listLevelWarm } from '@/api/push';

export const drillsStore = createGlobalState(() => {
  // 字典-气象类型
  const typeCodeOptions = ref<Global.Option[]>([]);
  const typeCodeEnum = ref<Record<string, any>>({});
  const getTypeCodeDict = () => {
    getDictType('sys_meteorological_type').then((res: any) => {
      typeCodeOptions.value = listToOption(
        res.data as any[],
        'dictLabel',
        'dictValue',
      );
      typeCodeEnum.value = listToEnum(
        res.data as any[],
        'dictValue',
        'dictLabel',
      );
    });
  };

  // 字典-气象类型
  const levelCodeOptions = ref<Global.Option[]>([]);
  const levelCodeEnum = ref<Record<string, any>>({});
  const getLevelCodeDict = () => {
    getDictType('sys_response_level').then((res: any) => {
      levelCodeOptions.value = listToOption(
        res.data as any[],
        'dictLabel',
        'dictValue',
      );
      levelCodeEnum.value = listToEnum(
        res.data as any[],
        'dictValue',
        'dictLabel',
      );
    });
  };

  // 字典-设备类型
  const deviceModeOptions = ref<Global.Option[]>([]);
  const deviceModeEnum = ref<Record<string, any>>({});
  const getDeviceModeDict = () => {
    getDictType('sys_device_mode').then((res: any) => {
      deviceModeOptions.value = listToOption(
        res.data as any[],
        'dictLabel',
        'dictValue',
      );
      deviceModeEnum.value = listToEnum(
        res.data as any[],
        'dictValue',
        'dictLabel',
      );
    });
  };

  // 获取-设备型号
  const getModelOptions = ref<Global.Option[]>([]);
  const getModelEnum = ref<Record<string, any>>({});
  const getDeviceModels = async () => {
    const res = await getModel();
    if (res.code == 200) {
      getModelOptions.value = listToOption(
        res.data as any[],
        'model',
        'modelId',
      );
      getModelEnum.value = listToEnum(res.data as any[], 'modelId', 'model');
    }
  };

  // 获取-项目名称
  const projectList = ref([]);
  const getProjectList = async () => {
    const res = await getProjectTree();
    if (res.code == 200) {
      projectList.value = addDisableProperty(res.data);
    }
  };

  // 字典-信息类型
  const eventTypeOptions = ref<Global.Option[]>([]);
  const eventTypeEnum = ref<Record<string, any>>({});
  const getEventTypeDict = () => {
    getDictType('sys_event_type').then((res: any) => {
      eventTypeOptions.value = listToOption(
        res.data as any[],
        'dictLabel',
        'dictValue',
      );
      eventTypeEnum.value = listToEnum(
        res.data as any[],
        'dictValue',
        'dictLabel',
      );
    });
  };
  // 获取预警配置列表
  const warmList: any = ref([]);
  const getWarmList = async () => {
    const res: any = await listLevelWarm();
    if (res.code == 200) {
      let obj = {
        label: '自动规则',
        value: '',
      };
      const list = res.data || [];
      warmList.value = list.map((item: any) => {
        item.levelConfigVOList.forEach((list: any) => {
          if (list.intensity) {
            list.intensity = list.intensity.split(',').map(Number);
          }
        });
        return {
          ...item,
          value: item.name,
          label: item.name,
        };
      });
      warmList.value.unshift(obj);
    }
    console.log(warmList.value, 'warmListwarmListwarmList');
  };

  const refreshStore = {
    weatherDrill() {
      getTypeCodeDict();
      getLevelCodeDict();
    },
    quickReport() {
      getDeviceModeDict();
      getDeviceModels();
      getProjectList();
    },
    warningDrill() {
      getDeviceModeDict();
      getEventTypeDict();
      getWarmList();
      getProjectList();
      getDeviceModels();
    },
  };
  return {
    refreshStore,
    typeCodeOptions,
    typeCodeEnum,
    levelCodeOptions,
    levelCodeEnum,
    deviceModeOptions,
    deviceModeEnum,
    getModelOptions,
    getModelEnum,
    projectList,
    eventTypeOptions,
    eventTypeEnum,
    warmList,
  };
});
